<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px" class="form-flex">
      <el-row :gutter="20">
        <el-col :span="6" :xs="24">
          <el-form-item label="类型代码" prop="typeCode">
            <el-input
              v-model="queryParams.typeCode"
              placeholder="请输入类型代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="类型名称" prop="typeName">
            <el-input
              v-model="queryParams.typeName"
              placeholder="请输入类型名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['workorder:workOrderType:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['workorder:workOrderType:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['workorder:workOrderType:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
                   @click="handleExport" v-hasPermi="['workorder:workOrderType:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workOrderTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="图标" align="center" prop="typeIcon">
        <template slot-scope="scope">
          <svg-icon :icon-class="scope.row.typeIcon" />
        </template>
      </el-table-column>
      <el-table-column label="类型代码" align="center" prop="typeCode"/>
      <el-table-column label="类型名称" align="center" prop="typeName"/>
      <el-table-column label="是否回访" align="center" prop="whereVisit">
        <template slot-scope="scope">
          {{ dict.label.sys_yes_no[scope.row.whereVisit] }}
        </template>
      </el-table-column>
      <el-table-column label="关联表单" align="center" prop="associationForm">
        <template slot-scope="scope">
          {{ dict.label.association_form[scope.row.associationForm] }}
        </template>
      </el-table-column>
      <el-table-column label="关联回单" align="center" prop="associationReceipt">
        <template slot-scope="scope">
          {{ dict.label.association_receipt[scope.row.associationReceipt] }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workorder:workOrderType:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['workorder:workOrderType:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <WorkOrderTypeForm ref="formRef" @cancel="getList"/>
  </div>
</template>

<script>

  import {listMixin} from "@/utils/mixins/listMixin";
  import {listWorkOrderType, delWorkOrderType, exportWorkOrderType} from "@/api/workorder/workOrderType";
  import WorkOrderTypeForm from "./form.vue"

  export default {
    name: "WorkOrderType",
    components: {WorkOrderTypeForm},
    mixins: [listMixin],
    dicts: ['sys_yes_no','association_form','association_receipt'],
    data() {
      return {
        // 工单类型表格数据
        workOrderTypeList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          typeCode: null,
          typeName: null,
          typeIcon: null,
          whereVisit: null,
          associationForm: null,
          associationReceipt: null,
        },
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询工单类型列表 */
      getList() {
        this.loading = true;
        listWorkOrderType(this.queryParams).then(response => {
          this.workOrderTypeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs.formRef.show({"type": "ADD"});
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        const id = row.id || this.ids
        const option = {
          "type": "UPDATE",
          "id": id
        }
        this.$refs.formRef.show(option);
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除工单类型编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delWorkOrderType(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有工单类型数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportWorkOrderType(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {
        });
      }
    }
  };
</script>
